from fastapi import APIRouter, Query,UploadFile
from tortoise.expressions import Q

from app.controllers.customer.users import users_controller
from app.controllers.saas.user_log import user_log_controller
from app.schemas.base import Success, SuccessExtra,Fail
from app.schemas.mini import *
from app.core.dependency import DependCustomerAuth
from app.core.ctx import CTX_USER_ID
from app.schemas.saas import UserLogCreate, UserLogUpdate
from app.utils.upload import upload_image,upload_file

mini_saas_user_log_router = APIRouter()
model_name = "员工日志"



@mini_saas_user_log_router.get("/list", summary=f"查看{model_name}列表", dependencies=[DependCustomerAuth])
async def do_mini_alert_list(
        page: int = Query(1, description="页码"),
        page_size: int = Query(10, description="每页数量"),
):
    user_id = CTX_USER_ID.get()
    user_data = await users_controller.get(user_id)
    user_id = user_data.user_id
    q = Q(user_id=user_id)
    total, data = await user_log_controller.do_list(page=page, page_size=page_size, search=q)
    for item in data:

        item["name"] = "服务日志"
        item["desc"] = item["data"]
    return SuccessExtra(data=data, total=total, page=page, page_size=page_size)

@mini_saas_user_log_router.post("/create", summary=f"创建员工日志")
async def do_create(
        data_in: UserLogCreate,
):
    ret = await user_log_controller.do_create(obj_in=data_in)
    if ret:
        return Success(msg="创建成功")
    else:
        return Fail(msg="创建失败")


@mini_saas_user_log_router.post("/update", summary=f"更新员工日志")
async def do_update(
        category_in: UserLogUpdate,
):
    await user_log_controller.do_update(obj_in=category_in)
    return Success(msg="更新成功")


@mini_saas_user_log_router.delete("/delete", summary=f"删除员工日志")
async def do_delete(
        data_id: int = Query(..., description="ID"),
):
    await user_log_controller.do_delete(data_id=data_id)
    return Success(msg="删除成功")